package com.banshuibao.bsb_android.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.AddressModel;
import com.banshuibao.bsb_android.model.ExpertTreeModel;
import com.banshuibao.bsb_android.model.RegionModel;
import com.banshuibao.bsb_android.util.PopuWindowsUtil;
import com.banshuibao.bsb_android.util.XToast;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

public class RecorderTypeActivity extends BaseActivity implements View.OnClickListener {

    private LinearLayout indeustry, category, special, region;
    private TextView indeustry_tv, category_tv, special_tv, region_tv;
    private PopupWindow popupWindow;
    private ExpertTreeModel expertTreeModel;
    private List<AddressModel> list;
    private String type = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recorder_type);
        initView();
        getTreeData();
    }

    public void initView() {
        setTitleText("请选择录音类型");
        indeustry = findViewById(R.id.industry);
        indeustry_tv = findViewById(R.id.industry_tv);
        category = findViewById(R.id.category);
        category_tv = findViewById(R.id.category_tv);
        special = findViewById(R.id.special);
        special_tv = findViewById(R.id.special_tv);
        region = findViewById(R.id.region);
        region_tv = findViewById(R.id.region_tv);
        indeustry.setOnClickListener(this);
        category.setOnClickListener(this);
        special.setOnClickListener(this);
        region.setOnClickListener(this);
        list = new ArrayList<>();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.industry:
                type ="industry";
                indeustry_tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.up, 0);
                if (expertTreeModel != null && expertTreeModel.getData().get(0).getChildren() != null) {
                    showDialog(view, expertTreeModel.getData().get(0).getChildren(), null, indeustry_tv);
                } else {
                    exception();
                }

                break;
            case R.id.category:
                type ="category";
                category_tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.up, 0);
                if (expertTreeModel != null && expertTreeModel.getData().get(1).getChildren() != null) {
                    showDialog(view, expertTreeModel.getData().get(1).getChildren(), null, category_tv);
                } else {
                    exception();
                }

                break;
            case R.id.special:
                type = "special";
                special_tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.up, 0);
                if (expertTreeModel != null && expertTreeModel.getData().get(2).getChildren() != null) {
                    showDialog(view, expertTreeModel.getData().get(2).getChildren(), null, special_tv);
                } else {
                    exception();
                }

                break;
            case R.id.region:
                type = "region";
                region_tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.up, 0);
                if (list != null && list.size() > 0) {
                    showDialog(view, null, list, region_tv);
                } else {
                    exception();
                }

                break;
        }
    }

    public void setAddress(TreeMap<String, String> hashMap) {
        //遍历集合得到业务id+名称
        for (TreeMap.Entry<String, String> entry : hashMap.entrySet()) {
            list.add(new AddressModel(entry.getValue(), entry.getKey(),""));
        }

    }

    public void exception() {
        XToast.showToast(RecorderTypeActivity.this, "获取数据失败，请检查网络");
        defultData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (popupWindow != null) popupWindow.dismiss();
    }

    /**
     * 展示弹窗
     */
    public void showDialog(View view, List<ExpertTreeModel.DataBean.ChildrenBean> list, List<AddressModel> addressModelList, final TextView tv) {
        PopuWindowsUtil popuWindowsUtil = new PopuWindowsUtil(this);
        if (list != null) {
            popupWindow = popuWindowsUtil.getP(list, null, "");
        } else if (addressModelList != null) {
            popupWindow = popuWindowsUtil.getP(null, addressModelList, "address");
        }
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                indeustry_tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.down, 0);
                category_tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.down, 0);
                region_tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.down, 0);
                special_tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.down, 0);
            }
        });
        popupWindow.showAsDropDown(view);
        popuWindowsUtil.setContent(new PopuWindowsUtil.ItemOnClickListener() {
            @Override
            public void getContent(String name, String uuid) {
                if (!TextUtils.isEmpty(name)) {
                    defultData();
                    AddressModel addressModel = null;
                    if (!TextUtils.equals("全国", name)) {
                        addressModel = new AddressModel(name, uuid, type);
                    } else {
                        addressModel = new AddressModel(name, "00",type);
                    }
                    Intent intent = new Intent(RecorderTypeActivity.this, RecorderContentActivity.class);
                    intent.putExtra("type", addressModel);
                    setResult(RESULT_OK, intent);
                    finish();
                }
                if (popupWindow != null) popupWindow.dismiss();
            }
        });
    }

    /**
     * 默认值
     */
    public void defultData() {
        indeustry_tv.setText("行业");
        indeustry_tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.down, 0);
        category_tv.setText("税种");
        category_tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.down, 0);
        special_tv.setText("专题");
        special_tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.down, 0);
        region_tv.setText("地区");
        region_tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.down, 0);
    }

    public void getTreeData() {
        /**
         * 行业税种专题数据
         */
        HttpUtil.doPost(Constants.Url.EXPERT_TREE, null, new HttpResponse(this, ExpertTreeModel.class) {
            @Override
            public void onResult(Object obj) {
                expertTreeModel = (ExpertTreeModel) obj;
            }
        });

        /**
         * 地区数据
         */
        HttpUtil.doPost(Constants.Url.EXPERT_ADDRESS, null, new HttpResponse(this, RegionModel.class) {
            @Override
            public void onResult(Object obj) {
                RegionModel regionModel = (RegionModel) obj;
                TreeMap<String, String> json = regionModel.getData();
                setAddress(json);
            }
        });
    }

}
